package com.chis.jmdatatimer.comm;

import com.chis.jm.comm.IPlugin;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @Description
 * @Company zwx
 * @author wlj
 * @version 2018年01月25日
 */
public class ServiceConsumer implements Serializable {
	/** 日志 */
	private static Logger LOGGER = LogManager.getLogger(ServiceConsumer.class);
	/** 单例对象 */
	private static ServiceConsumer instance = new ServiceConsumer();
	/** 接口服务<接口名称，服务对象> */
	private Map<String, IPlugin> pluginMap = new HashMap<String, IPlugin>();
	/** 上下文 */
	private static ClassPathXmlApplicationContext context;

	private ServiceConsumer() {
		this.initSessionBean();
	}

	public static ServiceConsumer getInstance() {
		return instance;
	}

	/**
	 * 初始化
	 */
	private void initSessionBean() {
		try {
			context = new ClassPathXmlApplicationContext(new String[] {"consumer_jmhivecon_head.xml", "consumer_jmhivecon_service.xml"});
			context.start();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage(), e);
		}
	}

	/**
	 * 获取插件
	 * @param inst 服务名称
	 * @return 服务
	 */
	public IPlugin getPlugin(String inst) {
		if (pluginMap.containsKey(inst)) {
			return this.pluginMap.get(inst);
		} else {
			IPlugin bean = (IPlugin) context.getBean(inst);
			if (bean != null) {
				pluginMap.put(inst, bean);
			}
			return bean;
		}
	}

}